package se.energimyndigheten.lampguiden.info;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;

import se.energimyndigheten.lampguiden.R;
import se.energimyndigheten.lampguiden.DashboardActivity;

public class InfoWebActivity extends SherlockFragmentActivity {

    public static final String EXTRA_VIEW = "view";
    public static final String EXTRA_MENUSELECTION = "menuSelection";
    public static final String EXTRA_TITLE = "title";
    
    private static final String TAG = InfoWebActivity.class.getSimpleName();

    private int mInfoId;
    private WebView mWebView;
    private int mSelectedActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setIcon(R.drawable.ic_actionbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // validate intent
        try {
            mInfoId = getIntent().getExtras().getInt(EXTRA_VIEW);
            mSelectedActivity = getIntent().getExtras().getInt(EXTRA_MENUSELECTION, -1);
            String title = getIntent().getStringExtra(EXTRA_TITLE);
            if (title != null) {
                setTitle(title);
            }
        } catch (NullPointerException npe) {
            Log.e(TAG, "Missing intent or expected extra.");
            finish();
            return;
        }

        setContentView(R.layout.webview_activity);

        mWebView = (WebView) findViewById(R.id.webview);
        loadHtmlFile();
    }
    
    /*** Handle action bar app icon click events. Go to start activity (dashboard). */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        final int menuId = item.getItemId();
        switch (menuId) {
        case android.R.id.home:
            // home icon in action bar clicked; go home
            Intent intent = new Intent(this, DashboardActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private String urlInfo(String tail) {
    	return "file:///android_asset/submodule_html/Information/"+tail;
    }

    private String urlBest(String tail) {
    	return "file:///android_asset/submodule_html/Basta_ljuset/"+tail;
    }
    
    private void loadHtmlFile() {
        Log.v(TAG, "loadHtmlFile");
        if (mSelectedActivity == DashboardActivity.ACTIVITY_LIGHTING) {
        	 switch (mInfoId) {
        	 case R.string.best_bath:
        		 mWebView.loadUrl(urlBest("sv_Badrumsljus.html"));
        		 break;
/* 	         case R.string.best_cooking:
 	        	 mWebView.loadUrl(urlBest("sv_Matlagningsljus.html"));
 	        	 break; */
 	         case R.string.best_dimmer:
 	        	 mWebView.loadUrl(urlBest("sv_Dimmerljus.html"));
 	        	 break;
 	         case R.string.best_dinner:
 	        	 mWebView.loadUrl(urlBest("sv_Middagsljus.html"));
 	        	 break;
 	         case R.string.best_general:
 	        	 mWebView.loadUrl(urlBest("sv_Allmanljus.html"));
 	        	 break;
 	         case R.string.best_mood:
 	        	 mWebView.loadUrl(urlBest("sv_Stamningljus.html"));
 	        	 break;
 	         case R.string.best_read:
 	        	 mWebView.loadUrl(urlBest("sv_Lasljus.html"));
 	        	 break;
 	         case R.string.best_work:
 	        	 mWebView.loadUrl(urlBest("sv_Arbetsljus.html"));
 	        	 break;
 	        default:
	            mWebView.loadUrl("file::///android_asset/404.html");
        	 }
        } else {
	        switch (mInfoId) {
	        case R.string.info_types:
	            mWebView.loadUrl(urlInfo("sv_Beskrivning_av_lamptyper.html"));
	            break;
	        case R.string.info_recycling:
	            mWebView.loadUrl(urlInfo("sv_Atervinning_och_miljo.html"));
	            break;
	        case R.string.info_definitions:
	            mWebView.loadUrl(urlInfo("sv_Ord_och_begrepp.html"));
	            break;        	
	        case R.string.info_why_cancel:
	            mWebView.loadUrl(urlInfo("sv_Varfor_fasas_glodlampan_ut.html"));
	            break;
	        case R.string.info_legal:
	            mWebView.loadUrl(urlInfo("sv_Konsumentratt.html"));
	            break;
	        case R.string.info_package:
	            mWebView.loadUrl(urlInfo("sv_Vad_star_pa_forpackningen.html"));
	            break;
	        case R.string.info_about:
	            mWebView.loadUrl(urlInfo("sv_Om_Lampguiden.html"));
	            break;
	        case R.string.info_energimyndigheten:   
	            mWebView.loadUrl(urlInfo("sv_Om_Energimyndigheten.html"));
	            break;
	        default:
	            mWebView.loadUrl("file::///android_asset/404.html");
	        }
        }
    }
}
